<template>
  <div class="business-payment-container">
    <!-- <top-tip :tipcontent="tipcontent" /> -->
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-space wrap :size="size">
            <!-- <el-form-item>
              <el-date-picker
                v-model="queryForm.orderDate"
                type="date"
                placeholder="请选择到单录入时间"
              />

              <DateSelect ref="DateSelectRef" @get-date="getDate" />
            </el-form-item>
            <el-form-item class="form-item">
              <el-cascader
                v-model="queryForm.city"
                :options="cityOptions"
                :props="citySelect"
                placeholder="选择地区"
                clearable
              />
            </el-form-item> -->
            <el-form-item>
              <el-input
                v-model.trim="queryForm.searchKey"
                clearable
                placeholder="按客户名称"
              />
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.myCompanyName"
                clearable
                placeholder="按我方开票单位"
              />
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.pickStaff"
                clearable
                placeholder="按签约人"
              />
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.orderCode"
                clearable
                placeholder="按合同编号"
              />
            </el-form-item>
            <el-form-item>
              <el-button :icon="Search" type="primary" @click="queryData">
                查询
              </el-button>
            </el-form-item>
          </el-space>
        </el-form>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-tabs
      v-model="queryForm.tab"
      type="border-card"
      @tab-click="handleTabClick"
    >
      <el-tab-pane label="新单待确认开票" name="new">
        <el-table
          v-loading="listLoading"
          :data="list"
          width="100%"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column align="center" show-overflow-tooltip type="selection" /> -->
          <el-table-column
            align="center"
            label="ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column align="center" label="到单录入时间" width="120">
            <template #default="{ row }">
              <div>
                {{ row.createTime.split(' ')[0] }}
                <br />
                {{ row.createTime.split(' ')[1] }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="left" label="客户名称">
            <template #default="{ row }">
              {{ row.CustomerAllName }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="纳税人识别号"
            show-overflow-tooltip
            width="200"
          >
            <template #default="{ row }">
              {{ row.Invoicing[0].CustomerLicenseCode }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="开票类型"
            show-overflow-tooltip
            width="80"
          >
            <template #default="{ row }">
              <div v-if="row.Invoicing[0].InvoicingType == 1">普票</div>
              <div v-if="row.Invoicing[0].InvoicingType == 2">专票</div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="开票金额"
            show-overflow-tooltip
            width="100"
          >
            <template #default="{ row }">
              {{ row.Invoicing[0].InvoicingMoney }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="签约业务类型"
            prop="BusinessOrderName"
            show-overflow-tooltip
          />
          <el-table-column
            align="center"
            label="签约人"
            prop="SaleStaffName"
            show-overflow-tooltip
          />
          <el-table-column align="left" label="我方开票单位">
            <template #default="{ row }">
              <div v-for="(item, index) in row.Invoicing" :key="index">
                {{ item.MyCompanyName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="120">
            <template #default="{ row }">
              <el-button type="primary" @click="handleEdit(row)">
                审核并确认
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
      <el-tab-pane label="已确认开票" name="reviewed">
        <el-table
          v-loading="listLoading"
          :data="list"
          width="100%"
          @selection-change="setSelectRows"
        >
          <!-- <el-table-column align="center" show-overflow-tooltip type="selection" /> -->
          <el-table-column
            align="center"
            label="ID"
            prop="OrderID"
            show-overflow-tooltip
            width="80"
          />
          <el-table-column align="center" label="到单录入时间" width="120">
            <template #default="{ row }">
              <div>
                {{ row.createTime.split(' ')[0] }}
                <br />
                {{ row.createTime.split(' ')[1] }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="left" label="客户名称" width="220">
            <template #default="{ row }">
              <div>{{ row.CustomerAllName }}</div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="纳税人识别号"
            show-overflow-tooltip
          >
            <template #default="{ row }">
              {{ row.Invoicing[0].CustomerLicenseCode }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="开票类型"
            show-overflow-tooltip
            width="80"
          >
            <template #default="{ row }">
              <div v-if="row.Invoicing[0].InvoicingType == 1">普票</div>
              <div v-if="row.Invoicing[0].InvoicingType == 2">专票</div>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="开票金额"
            show-overflow-tooltip
            width="100"
          >
            <template #default="{ row }">
              {{ row.Invoicing[0].InvoicingMoney }}
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="签约业务类型"
            prop="BusinessOrderName"
            show-overflow-tooltip
            width="200"
          />
          <el-table-column
            align="center"
            label="签约人"
            prop="SaleStaffName"
            show-overflow-tooltip
          />
          <el-table-column align="center" label="我方开票单位">
            <template #default="{ row }">
              <div v-for="(item, index) in row.Invoicing" :key="index">
                {{ item.MyCompanyName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="确认开票人">
            <template #default="{ row }">
              <div v-for="(item, index) in row.Invoicing" :key="index">
                {{ item.InvoicStaffName }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="确认开票时间" width="120">
            <template #default="{ row }">
              <div v-for="(item, index) in row.Invoicing" :key="index">
                {{ item.InvoicTime.split(' ')[0] }}
                <br />
                {{ item.InvoicTime.split(' ')[1] }}
              </div>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="100">
            <template #default="{ row }">
              <el-button type="primary" @click="handleEdit(row)">
                查看详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          :current-page="queryForm.pageNo"
          :layout="layout"
          :page-size="queryForm.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </el-tab-pane>
    </el-tabs>
    <edit ref="editRef" @fetch-data="fetchData" />
  </div>
</template>

<script>
  // import DateSelect from '/src/components/DateSelect.vue'

  import { CitySelect } from '@/api/common'
  import { getBusinessList } from '@/api/businessOrder'
  import Edit from './components/BusinessInvoicingEdit'
  // import TopTip from '@/components/TopTip'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'Invoicing',
    components: { Edit },
    setup() {
      const router = useRouter()
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        DateSelectRef: null,
        tipcontent: {
          title: '关于开具合同业务发票',
          memo:
            '本页用于为合同业务单开具发票；默认显示新提单未确认开发票的合同业务单；<br>' +
            '点开需要审核并开具发票的业务合同单，即可查看此业务合同的具体详情，同时可以看到相关开票信息；<br>' +
            '点击开票按扭，填写发票信息，并上传发票文件，提交确认已开票，发票即发送到邮箱，或短信链接通知到相关人员；',
        },
        editRef: null,
        list: [],

        cityOptions: [],

        citySelect: { checkStrictly: true, multiple: true },

        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          dataType: 'invoicing',
          tab: 'new',
          customerid: 0,
          pageNo: 1,
          pageSize: 10,
          searchKey: '',
        },
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const revieworder = (row) => {
        router.push({
          path: 'reviewOrder',
          query: { businessid: row.businessid },
        })
      }
      const handleEdit = (row) => {
        router.push({
          path: 'invoicingOrder',
          query: { orderid: row.OrderID },
        })
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }

      const handleTabClick = (tab) => {
        console.log(tab.props.name)
        state.queryForm.tab = tab.props.name
        fetchData()
      }

      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getBusinessList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
        getCitySelect()
      })

      const getCitySelect = async () => {
        const {
          data: { cityOptions },
        } = await CitySelect()
        state.cityOptions = cityOptions
        console.log('列表1', cityOptions)
      }

      const getDate = (data) => {
        state.queryForm.orderDate = data
        console.log(data, '子组件传过来的时间数组')
      }

      return {
        ...toRefs(state),
        getDate,
        setSelectRows,
        revieworder,
        handleEdit,
        getCitySelect,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        handleTabClick,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>

<style></style>
